package com.gxa.servlet.staffservlet;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxa.pojo.Result;
import com.gxa.pojo.staff.UserStaff;
import com.gxa.service.staffservice.StaffService;
import com.gxa.service.staffservice.impl.StaffServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/selectAllServlet")
public class SelectAllServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/json;charset=utf-8");
        //执行查找方法
        StaffService staffService = new StaffServiceImpl();
        ObjectMapper objectMapper = new ObjectMapper();
        List<UserStaff> all = staffService.findAll();
       //返回状态和信息
        Map<String,Object> map = new HashMap<>();
        String fl = null;
        if (all !=null){
            map.put("result", Result.ok("success",all));
            fl = objectMapper.writeValueAsString(map);
        }else {
            map.put("result", Result.error());
            fl = objectMapper.writeValueAsString(map);
        }
        PrintWriter writer = response.getWriter();
        writer.write(fl); //响应状态
        writer.flush();     //刷新流
        writer.close();     //关闭流
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
